package com.ruoyi.project.article.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.job.CronUtils;
import com.ruoyi.common.utils.job.ScheduleUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.article.domain.Article;
import com.ruoyi.project.article.service.IArticleService;

/**
 * 调度任务信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('article:list:index')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
    	System.out.println("fx article list Article="+article.toString());
    	
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        
        System.out.println("fx article list="+list.toString());
        
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
    	System.out.println("fx article export article="+article.toString());
    	System.out.println("fx article export response="+response.toString());
    	
        List<Article> list = articleService.selectArticleList(article);
        
        System.out.println("fx article export list="+list.toString());
        
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章列表");
    }

    
    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
    	System.out.println("fx article getInfo articleId="+articleId);
        return success(articleService.selectArticleById(articleId));
    }

    
    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Article article) throws SchedulerException, TaskException 
    {
    	System.out.println("fx article add article="+article.toString());
        article.setCreateBy(getUsername());
        return toAjax(articleService.insertArticle(article));
    }

    
    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('monitor:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody Article article) throws SchedulerException, TaskException
    {
    	System.out.println("fx article update article="+article.toString());
        return toAjax(articleService.updateArticle(article));
    }

    
    /**
     * 文章状态修改
    
    @PreAuthorize("@ss.hasPermi('monitor:article:changeStatus')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Article article) throws SchedulerException
    {
        Article newArticle = articleService.selectArticleById(article.getArticleId());
        newArticle.setStatus(article.getStatus());
        return toAjax(articleService.changeStatus(newArticle));
    }
     */

    /**
     * 文章立即执行一次
     
    @PreAuthorize("@ss.hasPermi('monitor:article:changeStatus')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody Article article) throws SchedulerException
    {
        boolean result = articleService.run(article);
        return result ? success() : error("任务不存在或已过期！");
    }
*/
    
    /**
     * 删除文章
      */
    @PreAuthorize("@ss.hasPermi('monitor:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds) throws SchedulerException, TaskException
    {
    	System.out.println("fx article delete articleIds="+Arrays.toString(articleIds));
    	
        articleService.deleteArticleByIds(articleIds);
        return success();
    }

}
